package com.tesis_2.DA;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.tesis_2.MO.MOLocal;
import com.tesis_2.MO.MOPlato;

public class DAOPlatos {

	
	public static ArrayList<MOPlato> getPlatos() throws JSONException {
		//DAORestaurants.lsRest = lsRest;
		
		ArrayList<MOPlato> lsPlatos = new ArrayList<MOPlato>();
		
	    try{

	    	JSONArray platos = (new JSONReaderArray().execute(DAOUtils.getServer()+ "/dishes.json")).get();
	    	
	    	if (platos == null) return lsPlatos;
	       	    	
	    	for (int i = 0; i < platos.length(); i++) {
	    		JSONObject jsonPlatos = platos.getJSONObject(i);
	    	    jsonPlatos = jsonPlatos.getJSONObject("dish");
	    	    //obtengo el restaurante
	    	    MOPlato objPlato = new MOPlato();
	    	    //entro al objeto y leo los atributos
	    	    
	    	    objPlato.setCost((float)jsonPlatos.getDouble("cost"));
	    	    objPlato.setCreated_at(DAOUtils.parseDateIso(jsonPlatos.getString("created_at")));
	    	    objPlato.setDescr(jsonPlatos.getString("descr"));
	    	    objPlato.setId(jsonPlatos.getInt("id"));
	    	    objPlato.setName(jsonPlatos.getString("name"));
	    	    objPlato.setRestaurant_id(jsonPlatos.getInt("restaurant_id"));
	    	    objPlato.setStatus(jsonPlatos.getInt("status"));
	    	    objPlato.setUpdated_at(DAOUtils.parseDateIso(jsonPlatos.getString("updated_at")));
	    	    objPlato.setRate((float)jsonPlatos.getDouble("rate"));
	    	    lsPlatos.add(objPlato);
	    	}
	 
	    }catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return lsPlatos;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return lsPlatos;
		}
		return lsPlatos;
		
	}
	
	public static ArrayList<MOPlato> getPlatosMasConsumidos(Integer RestId) throws JSONException {
		//DAORestaurants.lsRest = lsRest;
		
		ArrayList<MOPlato> lsPlatos = new ArrayList<MOPlato>();
		
	    try{

	    	JSONArray platos = (new JSONReaderArray().execute(DAOUtils.getServer()+ "/dishes/getDishesByConsume/"+RestId + ".json")).get();
	    	
	    	if (platos == null) return lsPlatos;
	       	    	
	    	for (int i = 0; i < platos.length(); i++) {
	    		JSONObject jsonPlatos = platos.getJSONObject(i);
	    	    jsonPlatos = jsonPlatos.getJSONObject("dish");
	    	    //obtengo el restaurante
	    	    MOPlato objPlato = new MOPlato();
	    	    //entro al objeto y leo los atributos
	    	    
	    	    objPlato.setCost((float)jsonPlatos.getDouble("cost"));
	    	    objPlato.setCreated_at(DAOUtils.parseDateIso(jsonPlatos.getString("created_at")));
	    	    objPlato.setDescr(jsonPlatos.getString("descr"));
	    	    objPlato.setId(jsonPlatos.getInt("id"));
	    	    objPlato.setName(jsonPlatos.getString("name"));
	    	    objPlato.setRestaurant_id(jsonPlatos.getInt("restaurant_id"));
	    	    objPlato.setStatus(jsonPlatos.getInt("status"));
	    	    objPlato.setUpdated_at(DAOUtils.parseDateIso(jsonPlatos.getString("updated_at")));
	    	    objPlato.setRate((float)jsonPlatos.getDouble("rate"));
	    	    lsPlatos.add(objPlato);
	    	}
	 
	    }catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return lsPlatos;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return lsPlatos;
		}
		return lsPlatos;
		
	}
	
	
	/*{"dish":{"cost":14.5,"created_at":"2013-02-11T13:56:45Z","descr":"fert"
	 * ,"id":13,"menu_id":1,"name":"ink","rate":3.0,
	 * "restaurant_id":1,"status":1,"updated_at":"2013-02-11T13:56:45Z"}}
	*/
	
	public static MOPlato createPlato(MOPlato objPlato) throws JSONException {
		//DAORestaurants.lsRest = lsRest;
		
		MOPlato Plato = new MOPlato();
		
	    try{
	    	/*
	    	 * http://192.168.1.106:3000/dishes/createDish/Hola/Plato/10,5/1.json
	    	*/
	    	String url = DAOUtils.getServer()+"/dishes/createDish" 
	    									+ "/" + objPlato.getName() +
	    									"/" + objPlato.getDescr() +
	    									"/" + objPlato.getCost().toString().replace(".", ",") +
	    									"/" + objPlato.getRestaurant_id() +
	    									".json";
	    	
	    	JSONObject jsondish = (new JSONReaderObject().execute(url)).get();
	    	
	    	if (jsondish == null) return Plato;
	       	    	
	    	
	    	JSONObject jsondishes = jsondish.getJSONObject("dish");
	    	   
	    	/*
	    	 * {"local":{"addr1":"add","addrs2":"add2",
	    	 * "capacity":10,"created_at":"2013-02-11T01:52:08Z"
	    	 * ,"descr":"mundo","id":13,"name":"hola","phone1":
	    	 * "1234","phone2":"4567","rate":3.0,"restaurant_id":1,
	    	 * "status":1,"updated_at":"2013-02-11T01:52:08Z"}}
	    	 * 
	    	 * 
	    	 */
	    	 Plato.setName(jsondishes.getString("name"));
	    	 Plato.setCost((float)jsondishes.getDouble("cost"));
	    	 Plato.setDescr(jsondishes.getString("descr"));
	    	 Plato.setId(jsondishes.getInt("id"));
	    	 Plato.setRate((float)jsondishes.getDouble("rate"));
	    	 Plato.setRestaurant_id(jsondishes.getInt("restaurant_id"));
	   
	   	    //[{"commentary":{"cmt":"Esto es un comentario de prueba.","created_at":"2012-12-11T22:52:43Z",
	   	    //"id":1,"local_id":2,"rate":3,"reservation_id":1,"restaurant_id":2,
    	    //"updated_at":"2012-12-11T22:52:43Z","user_id":1,"usrname":"Hunter"}}]
	    	    
	    	
	 
	    }catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Plato;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Plato;
		}
		
		return Plato;
		
	}
}
